package com.ss.android.account.b;

import android.content.Context;
import android.os.Handler;

import com.bytedance.common.utility.Logger;
import com.ss.android.account.bb;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.ActionData;
import com.ss.android.account.model.SharePlatform;
import com.ss.android.common.AbsApiThread;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.http.legacy.a.NameValueImpl;
import com.ss.android.model.AbsArticle;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class ActionThread2 extends AbsApiThread {
    private final Handler mHandler;
    private final Context mContext;
    private ActionData mActionData;
    private int d;

    public ActionThread2(Context context, Handler handler, String action, long ts, AbsArticle absArticle, long ad_id, List plats) {
        super("ActionThread2");
        this.d = 1;
        this.mContext = context.getApplicationContext();
        this.mHandler = handler;
        this.mActionData = new ActionData(action, ts, absArticle, plats);
        this.mActionData.mAdId = ad_id;
        this.d = 1;
    }

    private static String a(List arg4) {
        StringBuilder v2 = new StringBuilder();
        if (arg4 != null) {
            Iterator v3 = arg4.iterator();
            int v1;
            for (v1 = 1; v3.hasNext(); v1 = 0) {
                Object v0 = v3.next();
                if (v1 == 0) {
                    v2.append(",");
                }

                v2.append(((SharePlatform) v0).mName);
            }
        }

        return v2.toString();
    }

    private static boolean a(ActionData actionData, Context context, int arg13) {
        boolean v6;
        int v2_2;
        ArrayList<NameValueImpl> v3;
        int v10 = 18;
        int v1 = 1;
        boolean v0 = false;
        int v4 = 0;
        while (v4 < arg13) {
            actionData.mError = v10;
            try {
                if (android.support.design.a.isTextEmpty(actionData.mAction)) {
                    return false;
                }

                v3 = new ArrayList();
                v3.add(new NameValueImpl("action", actionData.mAction));
                v3.add(new NameValueImpl("group_id", String.valueOf(actionData.mItem.groupId)));
                v3.add(new NameValueImpl("item_id", String.valueOf(actionData.mItem.itemId)));
                v3.add(new NameValueImpl("aggr_type", String.valueOf(actionData.mItem.aggrType)));
                String v2_1 = actionData.mItem.tag;
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v3.add(new NameValueImpl("tag", v2_1));
                }

                if (actionData.mAdId > 0) {
                    v3.add(new NameValueImpl("ad_id", String.valueOf(actionData.mAdId)));
                }

                v2_1 = ActionThread2.a(actionData.mPlats);
                if (!android.support.design.a.isTextEmpty(v2_1)) {
                    v3.add(new NameValueImpl("platform", v2_1));
                    v2_2 = v1;
                } else {
                    v2_2 = 0;
                }
                String respString = NetworkUtils.executePost(20480, Spipe.dataItemActionUrl, v3);
                if (respString == null) {
                    return false;
                }

                if (respString.length() == 0) {
                    return false;
                }

                JSONObject responseJo = new JSONObject(respString);
                v6 = ActionThread2.isApiSuccess(responseJo);
                actionData.mSuccess = true;
                actionData.mExistAction = responseJo.optString("action_exist");
                if (!android.support.design.a.isTextEmpty(actionData.mExistAction)) {
                    actionData.mSuccess = false;
                }

                actionData.mDiggCount = responseJo.optInt("digg_count", -1);
                actionData.mBuryCount = responseJo.optInt("bury_count", -1);
                actionData.mRepinCount = responseJo.optInt("repin_count", -1);
                actionData.mCommentCount = responseJo.optInt("comment_count", -1);
                actionData.mLikeCount = responseJo.optInt("like_count", -1);
                if (v2_2 == 0 && !v6) {
                    actionData.mSuccess = false;
                    return v0;
                }

                if (v2_2 != 0) {
                    try {
                        actionData.mRepostSuccess = v6;
                        actionData.mExpiredPlatform = responseJo.optString("expired_platform", null);
                        if (!actionData.mRepostSuccess) {
                            actionData.mPostError = 18;
                            JSONObject dataJo = responseJo.optJSONObject("data");
                            if (dataJo != null) {
                                if (!"session_expired".equals(dataJo.optString("name"))) {
                                    Logger.e("snssdk", "item action error: " + respString);
                                } else if (!android.support.design.a.isTextEmpty(actionData.mExpiredPlatform)) {
                                    actionData.mPostError = 108;
                                } else {
                                    actionData.mPostError = 105;
                                }
                            }
                        }
                        return v1 > 0;
                    } catch (Throwable v2) {

                    }
                }
                return v1 > 0;
            } catch (Exception v2) {
                int v3_2 = bb.a().a(context, v2);
                if (v3_2 == 13 || v3_2 == 14) {
                    v3_2 = v1;
                } else {
                    v3_2 = 0;
                }
                if (v3_2 == 0) {
                    v1 = bb.a().a(context, v2);
                    actionData.mError = v1;
                    return v0;
                }
                ++v4;
                continue;
            }
        }

        return v0;
    }

    public final void run() {
        int v0 = ActionThread2.a(this.mActionData, this.mContext, this.d) ? 1005 : 1006;
        if (this.mHandler != null) {
            this.mHandler.sendMessage(this.mHandler.obtainMessage(v0, this.mActionData));
        }
    }
}

